<?php require ("../header.php"); ?>

<?php
	//check admin permission
	if ($ad_check<1){
			header ("Location:../../login.php?action=0");
	}
	//check all variable availibility and setting the right update type.
	if (isset($_POST['ad_name'])) {
		$ad_name=$_POST['ad_name'];
	}
	else { 
		echo "<center><br><br><br><br><br><br>PROFILE UPDATE FAILED.  PLEASE FILL ADMIN NAME FIELDS BEFORE SUBMIT.</CENTER><br><br><br><br><br><br><br><br>";
		echo "<META http-equiv=\"refresh\" content=\"2;URL=../admin.php\">";
	}
	if (isset($_POST['ad_no'])) {
		$ad_no=$_POST['ad_no'];
	}
	else { 
		echo "<center><br><br><br><br><br><br>PROFILE UPDATE FAILED.  PLEASE FILL STAFF NUMBER FIELDS BEFORE SUBMIT.</CENTER><br><br><br><br><br><br><br><br>";
		echo "<META http-equiv=\"refresh\" content=\"2;URL=../admin.php\">";
	}
	if (isset($_POST['ad_department'])) {
		$ad_department=$_POST['ad_department'];
	}
	else { 
		echo "<center><br><br><br><br><br><br>PROFILE UPDATE FAILED.  PLEASE FILL DEPARTMENT FIELDS BEFORE SUBMIT.</CENTER><br><br><br><br><br><br><br><br>";
		echo "<META http-equiv=\"refresh\" content=\"2;URL=../admin.php\">";
	}
	if ($_POST['ad_password1']!="" && $_POST['ad_password2']!="") {
		$ad_pass1=$_POST['ad_password1'];
		$ad_pass2=$_POST['ad_password2'];
		if ($ad_pass1==$ad_pass2) {
			$ad_password=$ad_pass1;
			$update=1;
		}
		else {
			echo "<center><br><br><br><br><br><br>PROFILE UPDATE FAILED.  NEW PASSWORD DO NOT MATCH.</CENTER><br><br><br><br><br><br><br><br>";
			require ("../footer.php");
			echo "<META http-equiv=\"refresh\" content=\"2;URL=../admin.php\">";
			exit();
		}
	}	elseif ($_POST['ad_password1']!="" && $_POST['ad_password2']=="") {
			echo "<center><br><br><br><br><br><br>PROFILE UPDATE FAILED.  PLEASE CONFIRM YOUR NEW PASSWORD BEFORE SUBMIT.</CENTER><br><br><br><br><br><br><br><br>";
			require ("../footer.php");
			echo "<META http-equiv=\"refresh\" content=\"2;URL=../admin.php\">";
			exit();
	}	elseif ($_POST['ad_password2']!="" && $_POST['ad_password1']=="") {
			echo "<center><br><br><br><br><br><br>PROFILE UPDATE FAILED.  PLEASE CONFIRM YOUR NEW PASSWORD BEFORE SUBMIT.</CENTER><br><br><br><br><br><br><br><br>";
			require ("../footer.php");
			echo "<META http-equiv=\"refresh\" content=\"2;URL=../admin.php\">";
			exit();
	}	
	else {
			$update=0;
	}
	//finish checking the variable.  All setting are sets and ready to start update the admin data.
	
	//update process begin
	if ($update == 0 ) {
		$sql_update="UPDATE admin SET ad_name='$ad_name' , ad_no='$ad_no' , ad_department='$ad_department' WHERE ad_username='$ad_username'";
		$update=mysql_query($sql_update) or die ("<center><br><br><br><br><br><br>PROFILE UPDATE FAILED.  
													DATA CANNOT STORED INTO DATABASE.</CENTER><br><br><br><br><br><br><br><br>");
		echo "<center><br><br><br><br><br><br>PROFILE UPDATE SUCCESS.  PLEASE WAIT TO BE REDIRECT TO ADMIN PAGE.</CENTER><br><br><br><br><br><br><br><br>";
		echo "<META http-equiv=\"refresh\" content=\"2;URL=../admin.php\">";
	} elseif ($update==1) {
		$sql_update="UPDATE admin SET ad_name='$ad_name' , ad_no='$ad_no' , ad_department='$ad_department' , ad_password=MD5('$ad_password') 
					WHERE ad_id='$id'";
		$update=mysql_query($sql_update) or die ("<center><br><br><br><br><br><br>PROFILE UPDATE FAILED.  
													DATA CANNOT STORED INTO DATABASE.</CENTER><br><br><br><br><br><br><br><br>");
		echo "<center><br><br><br><br><br><br>PROFILE UPDATE SUCCESS.  YOU HAVE TO RE-LOGIN WITH YOUR NEW PASSWORD.</CENTER><br><br><br><br><br><br><br><br>";
		$sid = session_id();
		$sql_check_session="DELETE FROM session WHERE session_code='$sid'";
		$check_session=mysql_query($sql_check_session);
		session_destroy();
		echo "<META http-equiv=\"refresh\" content=\"2;URL=../../login.php\">";
		
	}
	else {
		echo "<center><br><br><br><br><br><br>PROFILE UPDATE FAILED.  UNKNOWN ERROR.</CENTER><br><br><br><br><br><br><br><br>";
		echo "<META http-equiv=\"refresh\" content=\"2;URL=../admin.php\">";
	}
	//update process finish
?>			

<?php require ("../footer.php"); ?>